package com.zero.demo.jms.controller;

import com.alibaba.fastjson.JSON;
import com.zero.demo.jms.service.IExtParamComp;
import com.zero.demo.jms.util.BeetlUtils;
import groovy.lang.GroovyClassLoader;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zengzr
 * @desc
 * @Date 2019/5/6 19:09
 */
@RestController
@RequestMapping("template")
@Log4j2
public class TemplateController {

    @Autowired
    DefaultListableBeanFactory defaultList;

    @Autowired
    BeetlUtils beetlUtils;

    @GetMapping("index")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView("templateView.btl");
        return mv;
    }

    @PostMapping("build")
    public ResponseEntity<?> build(@RequestParam String dataJson,
                                   @RequestParam String extComp,
                                   @RequestParam String extParamJson,
                                   @RequestParam String template){
        Map<String, Object> extParam = null;
        Map<String,Object> dataMap = null;
        Map<String,Object> result = new HashMap<>();
        if (StringUtils.isNotEmpty(dataJson)) {
            dataMap = (Map<String, Object>) JSON.parse(dataJson);
        }
        if (StringUtils.isNotEmpty(extParamJson)) {
            extParam = (Map<String, Object>) JSON.parse(extParamJson);
        }
        if (StringUtils.isNotEmpty(extComp)) {
            Object iExtParamComp = null;
            try {
                GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
                //里面的文本是Java代码,但是我们可以看到这是一个字符串我们可以直接生成对应的Class<?>对象,而不需要我们写一个.java文件
                Class clazz = groovyClassLoader.parseClass(extComp);
                BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
                //defaultList.removeBeanDefinition("javaDynamic_st4page");
                defaultList.setAllowBeanDefinitionOverriding(true);
                defaultList.registerBeanDefinition("javaDynamic_st4page", beanDefinitionBuilder.getBeanDefinition());

                iExtParamComp = defaultList.getBean("javaDynamic_st4page");

                if(iExtParamComp instanceof IExtParamComp){
                    ((IExtParamComp)iExtParamComp).buildExtParam(extParam);
                }else {
                    throw  new RuntimeException("类文件内容异常，编译后的类需实现IExtParamComp或IExtParamComp4DB接口!");
                }
            } catch (Exception e) {
                throw new RuntimeException("动态组件执行异常!!", e);
            }
        }
        result.put("dataJson",dataMap);
        result.put("extParam",extParam);
        String renderResult = beetlUtils.buildString(result, template);
        log.info("返回字符串:{}",renderResult);
        return ResponseEntity.ok(renderResult);
    }

}
